/********************************************************************************
 * @Author: Zhenzhou Liu
 * @Date: 2024-12-29
 * @Description: Cg图论练习之有向图判环
 *******************************************************************************/
#include <iostream>
#include <vector>

using namespace std;

enum Color {
    WHITE,
    GRAY,
    BLACK
};

bool dfsJudgeCycle(const vector<vector<int>> & adj, int u, vector<Color> & color) {
    color[u] = GRAY;

    for(int v : adj[u]) {
        if(color[v] == GRAY) {
            return true;
        }
        if(color[v] == WHITE) {
            if(dfsJudgeCycle(adj, v, color)) {
                return true;
            }
        }
    }
    color[u] = BLACK;
    return false;
}

bool judgeCycle(const vector<vector<int>> & adj, int n) {
    vector<Color> color(n + 1, Color::WHITE);
    for (int u = 1; u <= n; u++) {
        if (color[u] == Color::WHITE) {
            if (dfsJudgeCycle(adj, u, color))
                return true;
        }
    }
    return false;
}

int main(int argc, char *argv[]) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n + 1);
    int u, v;
    for (int i = 0; i < m; i++) {
        cin >> u >> v;
        adj[u].push_back(v);
    }

    cout << (judgeCycle(adj, n) ? 1 : 0);
    return 0;
}

/*Test case1
3 3
1 2
2 3
3 1
*/
